// app.js
App({
  globalData: {
    userInfo: {},
    openid: 'zx052011-7gso84r5972c08ab'
  },
  //初始化
  onLaunch() {
    wx.cloud.init({
      env: 'zx052011-7gso84r5972c08ab',
      traceUser: true
    })
    this.globalData.userInfo = wx.getStorageSync('user')
    this.getOpenid();

    // if (wx.getStorageSync('user')) {
    //   wx.switchTab({
    //     url: '/pages/index/index',
    //   })
    // } else {
    //   wx.reLaunch({
    //     url: '/pages/login/login',
    //   })
    // }
  },
  //获取用户openid
  getOpenid: function () {
    var app = this;
    var openidStor = wx.getStorageSync('openid');
    if (openidStor) {
      console.log('本地获取openid:' + openidStor);
      app.globalData.openid = openidStor;
    } else {
      wx.cloud.callFunction({
        name: 'getOpenid',
        success(res) {
          var openid = res.result.openid;
          console.log('云函数获取openid成功', openid)
          wx.setStorageSync('openid', openid)
          app.globalData.openid = openid;
        },
        fail(res) {
          console.log('云函数获取失败', res)
        }
      })
    }
  },
  // 保存登录信息
  _saveUserInfo: function (user) {
    this.globalData.userInfo = user;
    wx.setStorageSync('user', user)
  },

  // 获取当前的年月日
  _getNianYuiRi() {
    var date = new Date();
    var year = date.getFullYear();
    var mon = date.getMonth() + 1;
    var day = date.getDate();
    var submitTime = "";
    submitTime += year + '-';
    if (mon >= 10) {
      submitTime += mon + '-';
    } else {
      submitTime += "0" + mon + '-';
    }
    if (day >= 10) {
      submitTime += day;
    } else {
      submitTime += "0" + day;
    }
    return submitTime
  }
})